import pymysql
from setting import *


class MySqlOperation:
    def __init__(self,
                 host=SQLHOST,
                 user=SQLUSER,
                 password=SQLPWD,
                 port=SQLPORT,
                 database=SQLDATABASE):
        self.host = host
        self.user =user
        self.pwd = password
        self.port =port
        self.database = database
        self.sqlconn = pymysql.connect(host=self.host,
                                  user=self.user,
                                  password=self.pwd,
                                  port=self.port,
                                  database=self.database)
        self.cur = self.sqlconn.cursor()

    def operation(self, cmd):
        self.cur.execute(cmd)

    def commit(self):
        self.sqlconn.commit()

    def rollback(self):
        self.sqlconn.rollback()

    def fetch_all(self):
        sql_result = self.cur.fetchall()
        return sql_result

    def close_all(self):
        self.cur.close()
        self.sqlconn.close()


if __name__ == '__main__':
    sql = MySqlOperation()
    sql.operation('select * from stu_info')
    print(sql.fetch_all()[0])
